package sy.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.BaseDaoI;

@SuppressWarnings("unchecked")
@Repository("BaseDao")
public class BaseDaoImpl<T> implements BaseDaoI<T> {
	@Autowired
	private SessionFactory sessionFactory;
 
	public Session getCurrenSession(){
		return sessionFactory.getCurrentSession();
	}
	@Override
	public Serializable save(T t) {
 		return sessionFactory.getCurrentSession().save(t);
	}

	@SuppressWarnings("unchecked")
	@Override
	public T get(String hql) {
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		List<T> list =query.list();
		if(list!=null&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public T get(String hql, Object[] parmas) {
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		if(parmas!=null&&parmas.length>0){
		    for(int i=0;i<parmas.length;i++){
		    	query.setParameter(i, parmas[i]);
		    }
		}
		List<T> list =query.list();
		if(list!=null&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public T get(String hql, Map<String,Object> params) {
  		Query q=this.getCurrenSession().createQuery(hql);
		if(params!=null&&!params.isEmpty()){
        	for(String key:params.keySet()){
			   q.setParameter(key, params.get(key));
          	}
        }
		List<T> list=q.list();
         if(list!=null&&list.size()>0){
         	return list.get(0);
        }
		return null;
	}

	@Override
	public void delete(T t) {
		this.getCurrenSession().delete(t);
		
	}

	@Override
	public void update(T t) {
		this.getCurrenSession().update(t);
		
	}
	@Override
	public void saveOrUpdate(T t) {
		this.getCurrenSession().saveOrUpdate(t);
		
	}
	@Override
	public List<T> find(String hql) {
		Query q=this.getCurrenSession().createQuery(hql);
		 
		return q.list();
	}
	@Override
	public List<T> find(String hql, Map<String, Object> params) {
		Query q=getQurey(hql,params);
		return q.list();
	}
	@Override
	public List<T> find(String hql, Map<String, Object> params, int page,
			int rows) {
		Query q=getQurey(hql,params);
 		return q.setFirstResult((page-1)*rows).setMaxResults(rows).list();
 
	}
	@Override
	public List<T> find(String hql, int page, int rows) {
		Query q=this.getCurrenSession().createQuery(hql);
		return q.setFirstResult((page-1)*rows).setMaxResults(rows).list();
	}
	@Override
	public Long count(String hql) {
		Query q=this.getCurrenSession().createQuery(hql);
 		return (Long)q.uniqueResult();
	}
	@Override
	public Long count(String hql, Map<String, Object> params) {
  		Query q=getQurey(hql,params);
 		return (Long)q.uniqueResult();
	}

	
	@Override
	public T get(Class<T> c, Serializable id) {
		
		return (T)this.getCurrenSession().get(c, id);
	}
	@Override
	public int executeHql(String hql) {
		Query query=this.getCurrenSession().createQuery(hql);
		return query.executeUpdate();
	}
	private Query getQurey(String hql, Map<String, Object> params){
		Query q=this.getCurrenSession().createQuery(hql);
		if(params!=null&&!params.isEmpty()){
        	for(String key:params.keySet()){
        	    Object value=params.get(key);
        	    if(value!=null&&!value.toString().trim().equals("")){
         	    	if(value instanceof String ){
        	         	q.setParameter(key, "%%"+value.toString().trim()+"%%");
        	        }else {
						q.setParameter(key, value);
					}
        		}
         	}
        }
  	  return q;
	}
}
